<?php
/**
 * Description of APi 微信服务号接口
 * @author zhang
 */
namespace Home\Controller;
use Think\Controller;
use Common\Extend\Weixin\Prpcrypt;
use Think\Log;

class ApiController extends Controller {


    public function __construct()
    {
        header("Content-Type: text/html; charset=UTF-8");
        _log(I('get.'),'verify_ticket');
    }

    /**
     * 微信公众号向第三方发送$encryptMsg，每10分钟发送一次
     * @return array
     */
    public function message() {

        require_once(APP_PATH."Common/Extend/Weixin/Wechat.class.php");
        $encryptMsg   =file_get_contents('php://input');
        _log($encryptMsg,'verify_ticket');
        $xml_tree = new \DOMDocument();
        $xml_tree->loadXML($encryptMsg);

        $xml_array	= $xml_tree->getElementsByTagName('Encrypt');
        $encrypt 	= $xml_array->item(0)->nodeValue;
        $Prpcrypt 	 	= new Prpcrypt('qZO3rnBcEsfPyiBvfb1y07mDmkbeT7widPtBLKmVHxq');
        $postData 		= $Prpcrypt->decrypt($encrypt,'wxd4941f0c17f04be4');
        if ($postData[0] != 0) {
            return $postData[0];
        }else{
            $msg = $postData[1];
            $xml = new \DOMDocument();
            $xml->loadXML($msg);
            $array_a = $xml->getElementsByTagName('InfoType');
            $infoType 		= $array_a->item(0)->nodeValue;
            if($infoType == 'unauthorized'){
                $array_b = $xml->getElementsByTagName('AuthorizerAppid');
                $AuthorizerAppid = $array_b->item(0)->nodeValue;
            }else if($infoType == 'component_verify_ticket'){
                $array_e = $xml->getElementsByTagName('ComponentVerifyTicket');
                $component_verify_ticket = $array_e->item(0)->nodeValue;
                $data['add_time']=time();
                $data['encryptMsg']=$component_verify_ticket;
                $wx_msg=M('wx_encrypt_msg')->where(array('appid'=>'wxd4941f0c17f04be4'))->save($data);
                if($wx_msg){
                    echo "success";
                }
            }
        }
    }

    public function callback() {

        _log($_REQUEST,'wx_mgs');
        //Log::write("接收结果:".var_export($_,true));
    }

}
